$.fn.gmapForm = function(options) {
		
	return this.each(function() {
		// Create Map
		var element = this;
		$.gmapForm.initialize(element);
		
	});
};


$.gmapForm = {
	initialize: function(element) {
		// Get latitude, longitude if they're already there
		
		var latitude = $(element).parent('section').children('.google-maps-location-latitude');
		var longitude = $(element).parent('section').children('.google-maps-location-longitude');
		var address = $(element).parent('section').children('.google-maps-location-address');
		if(latitude.val() && longitude.val() && address.val()) {
			$(element).gmaps({
				center: {
					latitude:latitude.val(),
					longitude:longitude.val()
				},
				markers:[{
					latitude:latitude.val(),
					longitude:longitude.val()
				}],
				zoom:13
			});
		}
		else if(address.val()) { // Geocode address
			$(element).gmaps({
				center: {
					address:address.val()
				},
				markers:[{
					address:address.val()
				}],
				onGeocode: function(location) {
					latitude.val(location.lat());
					longitude.val(location.lng());
				}
			});
		}
		else { // Load the map
			$(element).gmaps({
				geolocate: {
					geolocate:true
				}
			});
		}
		
		
		// Bind the address field
		address.typing({
			stop:function() {
				$(element).gmaps({
					center: {
						address:address.val()
					},
					markers:[{
						address:address.val()
					}],
					zoom:13,
					onGeocode:function(location) {
						latitude.val(location.lat());
						longitude.val(location.lng());
					}
				});
			},
			delay:1000
		});
	}
};